package com.nkbh.sevenday.myhttp;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.nkbh.sevenday.myhttp.http.CallBackResponse;
import com.nkbh.sevenday.myhttp.http.HttpManager;
import com.nkbh.sevenday.myhttp.http.OkHttp;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    String url = "http://www.baidu.com";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        HttpManager manager=new HttpManager();
        //get的方式
        manager.get(url,callBackResponse);
    }
    //处理返回的结果
    CallBackResponse callBackResponse=new CallBackResponse() {
        @Override
        public void faile(Exception exception) {
            Log.i(TAG, "faile: 失败");
        }

        @Override
        public void success(Response response, int sendCode) {
            try {
                Message msg=Message.obtain();
                msg.what=sendCode;
                msg.obj=response.body().string();
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    };
    //可以在handle中处理UI
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what== OkHttp.GET){
                Log.i(TAG, "handleMessage: get");
            }else if(msg.what== OkHttp.POST){
                Log.i(TAG, "handleMessage: post");
            }
        }
    };
}
